/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pojo.Doutor;
import pojo.Excessao;

/**
 *
 * @author Rodolfo
 */
@Stateless
public class GeraHorario implements GeraHorarioLocal {

    SimpleDateFormat sdfDiaSemana = new SimpleDateFormat("EEEE");
    @PersistenceContext
    private EntityManager manager;

    public GeraHorario() {
    }

    @Override
    public boolean verificaExcessaoData(Doutor doutor, Date data) {
        Query query = manager.createQuery(
                "select e FROM Excessao e where e.codDoutor = :doutor and e.data = :data and e.horario is null").setParameter("doutor", doutor);
        query.setParameter("data", data);
        if (!query.getResultList().isEmpty()){
                return true;
        }
        String dia_semana = sdfDiaSemana.format(data);
        Query query1 = manager.createQuery(
                "select e FROM Excessao e where e.codDoutor = :doutor and e.dia_semana = :dia_semana and e.horario is null").setParameter("doutor", doutor);
        query1.setParameter("dia_semana", dia_semana);
        if (!query1.getResultList().isEmpty()){
                return true;
        }
        return false;

    }
    
    @Override
        public boolean verificaExcessaoDataHora(Doutor doutor, Date data, Date horario) {
        Query query = manager.createQuery(
                "select e FROM Excessao e where e.codDoutor = :doutor and e.horario = :horario and e.dia_semana is null and e.data is null").setParameter("doutor", doutor);
        query.setParameter("horario", horario);
        if (!query.getResultList().isEmpty()){
                return true;
        }
        
        Query query1 = manager.createQuery(
                "select e FROM Excessao e where e.codDoutor = :doutor and e.horario = :horario and e.data = :data").setParameter("doutor", doutor);
        query1.setParameter("horario", horario);
        query1.setParameter("data", data);
        if (!query1.getResultList().isEmpty()){
                return true;
        }    
        String dia_semana = sdfDiaSemana.format(data);
        Query query2 = manager.createQuery(
                "select e FROM Excessao e where e.codDoutor = :doutor and e.horario = :horario and e.dia_semana = :dia_semana").setParameter("doutor", doutor);
        query2.setParameter("horario", horario);
        query2.setParameter("dia_semana", dia_semana);
        if (!query2.getResultList().isEmpty()){
                return true;
        } 
        return false;

    }

    @Override
    public List<String> geraHorariosDisponivel(Doutor doutor, Date data) {
        System.out.println("entrou aki getHorarioDisponivel");
        if(data.before(new Date())){
            return null;
        }
        
        if (verificaExcessaoData(doutor, data)){
            System.out.println("validor verdadeiro");
            return null;
        }
        System.out.println("passou do primeir ifff");
        List<String> listadeHorarios = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        


        Date horaInicial;
        Date horaFinal;
        horaInicial = doutor.getHorarioTrabInicial();
        horaFinal = doutor.getHorarioTrabFinal();

        while (horaFinal.after(horaInicial)) {
            System.out.println(sdf.format(horaInicial));
            if (!verificaExcessaoDataHora(doutor, data,horaInicial)){
                listadeHorarios.add(sdf.format(horaInicial));
                 System.out.println("adicionou horario");
            }
            
            horaInicial = new Date(horaInicial.getTime() + 30 * 60 * 1000);


        }
        System.out.println("adicionou horario");

        return listadeHorarios;
    

    }
    
    
}
